import SerialPort from "serialport";
import InterByteTimeout from './serialportParser'


import OMIT from "lodash/omit"
import KEYS from "lodash/keys"
import HASIN from 'lodash/hasIn'


var portObj = {}


export const getPortTable = function () {
    return new Promise((res, rej) => {
        SerialPort.list().then(
            ports => {
                let curPorts = []
                ports.forEach(element => {
                    let port = {
                        path: element.path,
                        open: false
                    }
                    curPorts.push(port)
                })
                res(curPorts)
            },
            err => rej(err)
        )
    })
}
export const openPort = function (path, cfg, callback) {
    //如果已经打开；就直接返回不再重新打开
    if (HASIN(portObj, path)) {
        return
    }
    const port = new SerialPort(path, cfg)
    console.log(port)
    if (port.isOpen && (port.path == path)) {
        //判断串口打开，不再执行打开动作，保存对象后，直接返回打开成功
        portObj[path] = port
        return callback(null)
    }
    //通常情况打开处理
    port.open(err => {
        if (err) {
            if(port.opening){
                portObj[path] = port
                return callback(null)
            }
            console.log(err)
        } else {
            portObj[path] = port
        }
        console.log(port)
        callback(err)
    })

}

export const closePort = function (path, callback) {
    console.log(path)
    console.log(portObj)
    KEYS(portObj).forEach(port => {
        if (port == path) {
            console.log(portObj[port].isOpen)
            if(portObj[port].isOpen){
            portObj[port].close(err => {
                callback(err)
                if (err) {
                    console.log(err)
                    return
                }
                console.log(portObj)
                portObj = OMIT(portObj, path)

            })
            }
        }
    })

}

export const rxPort = function (path, timer, callback) {
 
    KEYS(portObj).forEach(port => {
        if (port == path) {
            let value =  portObj[port]
            if (HASIN(value, "parser") && value.parser) {
                value.unpipe(value.parser)
                value.parser = null
            }
            value['parser'] = value.pipe(new InterByteTimeout({ interval: timer }))
            value.parser.on('data', callback)
        }
    })
}

export const txPort = function (path, buffer, encoding, callback) {
    KEYS(portObj).forEach(port => {
        if (port == path) {
            const result = portObj[port].write(buffer, encoding, (err, number) => {
                console.log("数据发送，进入回调")
                callback(err, number, result)
            })
        }
    })
}

export const pausePort = function (path) {
    KEYS(portObj).forEach(port => {
        if (port == path) {
            portObj[port].pause()
        }
    })
}

export const resumePort = function (path) {
    KEYS(portObj).forEach(port => {
        if (port == path) {
            portObj[port].resume()
        }
    })
}

export const upPortSettings = function (path, cfg, callback) {
    KEYS(portObj).forEach(port => {
        if (port == path) {
            portObj[port].update(cfg, (err) => {
                console.log(cfg)
                callback(err)
                console.log(portObj[port].settings)
            })
        }
    })
}
export const getPortSettings = function () {
    KEYS(portObj).forEach(port => {
        console.log(portObj[port].settings)
    })
}

